package com.viphuli.http.handler;

import java.util.List;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.BaseAdapter;

import com.offroader.R;
import com.offroader.utils.ViewUtils;
import com.viphuli.adapter.HealthListViewAdapter;
import com.viphuli.adapter.NurseListViewAdapter;
import com.viphuli.common.Constants;
import com.viphuli.fragment.HomeServiceFragment;
import com.viphuli.http.bean.page.HomeRoomServiceItemListPage;
import com.viphuli.http.bean.part.HomeRoomServiceItem;

public class HomeServiceHandler extends MyBaseHttpResponseHandler<HomeServiceFragment, HomeRoomServiceItemListPage> {

	@Override
	public void deal() {
		int serviceType = caller.getServiceType();
		List<HomeRoomServiceItem> list = page.getList();

		if (list != null && !list.isEmpty()) {

			caller.setPage(page.getPage());//设置页码
			caller.getList().addAll(list);//填充数据

			if (caller.getListView().getAdapter() == null) {//无Adapter,设置并填充数据后通知视图更新

				BaseAdapter adapter = null;

				switch (serviceType) {
				case Constants.HOME_SERVICE_HEALTH_PERSON:
					adapter = new HealthListViewAdapter(caller.getActivity(), caller.getList());
					break;

				case Constants.HOME_SERVICE_NURSE_PERSON:
					adapter = new NurseListViewAdapter(caller.getActivity(), caller.getList());
					break;

				case Constants.HOME_SERVICE_HEALTH_COMPANY:
					adapter = new NurseListViewAdapter(caller.getActivity(), caller.getList());
					break;

				default:
					break;
				}
				
				
				// 添加只是为了在ListView的setAdapter方法时将Adapter包装成HeaderViewListAdapter。因此并不需要footer，因此添加后再移除
				View mListViewFooter = LayoutInflater.from(caller.getActivity()).inflate(R.layout.listview_footer, null, false);
				caller.getListView().addFooterView(mListViewFooter);
				caller.getListView().setAdapter(adapter);
				caller.getListView().removeFooterView(mListViewFooter);

				caller.setBaseAdapter(adapter);

			} else {//填充数据后通知视图更新
				caller.getBaseAdapter().notifyDataSetChanged();
			}

		} else if (page.isFirstPage()) {//加载第一页无数据时，显示空界面
			caller.doShowEmpty();
		} else {
			ViewUtils.toast(R.string.list_no_more_data);
		}

	}
}
